package com.yc.spring.mvc.web;

import com.yc.spring.mvc.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
//类上加这个注解-》在地址栏后面加，一下所有方法的地址前都有.../test/...
@RequestMapping("test")
@RestController
public class HelloAction {
    //MediaType.TEXT_HTML_VALUE+";charset=utf-8"让页面可以访问中文
    @GetMapping(value = "hello",produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    public String sayHello(String name) {
        System.out.println("---------------hello--------------");
        return "Hello " + name;
    }
    //method = {RequestMethod.DELETE,RequestMethod.POST}-》，它表明该方法可以响应DELETE和POST请求。
    @RequestMapping(value = "demo",method = {RequestMethod.DELETE,RequestMethod.POST})
    public String demo(String name) {
        return "demo " + name;
    }
    //GetMapping->只能get请求
    @GetMapping("get")
    public String get(String name) {
        return "get " + name;
    }
    //PostMapping->post
    @PostMapping("post")
    public String post(String name) {
        return "post " + name;
    }
    //限定参数满足有a,b=1,c!=2
    @GetMapping(value = "param",params = {"a","b=1","c!=2"})
    public String param(String name) {
        return "param " + name;
    }
    //请求头限制connection=keep-alive
    @RequestMapping(value = "header",headers = {"connection=keep-alive"})
    public String header(String name) {
        return "header " + name;
    }
    @GetMapping("add")
    public int add(int a,int b){
        return a+b;
    }
    @GetMapping("sevlet")
    public Map<String,Object> servlet(
            HttpServletResponse response,
            HttpServletRequest request,
            HttpSession session){
        Map<String,Object> ret=new HashMap<>();
        ret.put("request",request);
        ret.put("session",session);
        ret.put("response",response);
        return ret;
    }
    @GetMapping("user")
    public User user(User user){
        user.setAge(user.getAge()+10);
        return user;
    }
    @GetMapping("sub/{c}")
    public int sub(@RequestParam("a") int x,
                   //required-》必须有的
                   @RequestParam(name="b",required = false,
                           defaultValue = "10") int y,
                   //地址里的
                   @PathVariable(value = "c") int z){
            return  x-y-z;
    }
    @GetMapping("headers")
    public String headers(
            @RequestHeader String Accept,
            @RequestHeader("Host") String host,
            @RequestHeader("Cookie") String cookie,
            @CookieValue String name,
            @CookieValue("myAge") String age
    ){
        return String.format("<h1>%s</h1>"
                +"<h1>%s</h1>"
                +"<h1>%s</h1>"
                +"<h1>%s</h1>"
                +"<h1>%s</h1>"
                ,Accept,host,cookie,name,age);
    }
    @GetMapping("toTest")
    public ModelAndView toTest(ModelAndView mav){
        mav.addObject("myname","李四");
        mav.setViewName("/test.jsp");
        return mav;
    }
}
